package com.gerryrun.itemscrollapplication.simple

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gerryrun.itemscrollapplication.databinding.ItemLayoutBinding
import com.gerryrun.workserviceapplication.simple.NestedAdapter

interface IItemClickCallBack {
    fun itemClickEdit(position: Int, data: Any)
    fun itemClickDelete(position: Int, data: Any)
    fun childItemClick(parentPosition: Int,childPosition: Int, data: Any)
}

class MyAdapter(private val data: List<MyData>, val itemClick: IItemClickCallBack) :
    RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(ItemLayoutBinding.inflate(LayoutInflater.from(parent.context)))
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val item = data[position]
        holder.bind(item)
    }

    override fun getItemCount(): Int = data.size

    inner class MyViewHolder(val itemViewBind: ItemLayoutBinding) :
        RecyclerView.ViewHolder(itemViewBind.root) {
        private val nestedList: RecyclerView = itemViewBind.nestedList

        fun bind(item: MyData) {
            // Bind your data to the view
            // Setup nested list
            nestedList.adapter = NestedAdapter(item.subItems)
            nestedList.layoutManager = LinearLayoutManager(itemView.context)

            //禁止滚动
            nestedList.isNestedScrollingEnabled = false

            itemViewBind.qqTouchItemView.let { qqTouchItemView ->
                qqTouchItemView.layoutBinding.apply {
                    title.text = "${item.title}+${item.title}"
                    editButton.setOnClickListener {
                        itemClick.itemClickEdit(adapterPosition, item)
                    }

                    deleteButton.setOnClickListener {
                        itemClick.itemClickDelete(adapterPosition, item)

                        qqTouchItemView.resetTitleViewScrollState()
                    }
                }
            }
        }
    }
}